/**
 * Created by H2 on 2016/1/29.
 *
 * 一切都还不算晚！
 */

$(function(){
    var userInfoDetailURL = "/api/client/agent/getAgent";
    var accountInfoURL = "/api/client/info/getAccountInfo";

    var personalMap = {
        "#personalTitle-temp" : "#personalTitle"
    };
    var agentDetailMap = {
        "#agentDetailMain-temp" : "#agentDetailMain"
    };

    //渲染个人基本信息
    doRender(
        accountInfoURL,
        {
            t : now()
        },
        personalMap,
        clickEvent
    );

    /**
     *  绑定下拉菜单事件
     **/
    function clickEvent(){
        /**
         * 异常判断
         **/
        $(".contentContainer").css("background","#002841");
        $(".personalAddTitle").html('<h3 style="text-align:center; margin: 5% 0; color: #fff;">没有数据</h3>');

        $(".personalContent > p").click(function(){
            $(this).next("div").slideToggle("normal");
            $(this).find("i").toggleClass("iconIndicateDown");
        });
    }

    /**
     *  绑定经纪人按钮，渲染经济人具体信息
     **/
    $(".btnMyAgent").click(function(){

        //风险评估消失，再渲染经纪人
        if($("#qn-result").css("display") == "block"){
            $("#qn-result").slideUp("slow");
        }

        //渲染经纪人信息
        doRender(
            userInfoDetailURL,
            {
                t : now()
            },
            agentDetailMap,
            showAgent
        );

        function showAgent(){
            if( $(".agentDetailHeader").html() == ""){
                $(".btnMyAgent").attr({"disabled":"disabled"});
            }
            $("#agentDetailMain").slideToggle("slow");
        }

    });


    /**
     *  绑定经纪人按钮
     **/
    $(".btnMyEvaluate").click(function(){

        //经纪人消失，在渲染风险评估
        if( $("#agentDetailMain").css("display") == "block"){
            $("#agentDetailMain").slideUp("slow");
        }

        //渲染评估风险及图表
        $render('#qn-result-temp', '#qn-result', null, initPage);
        $('#qn-result').slideToggle('slow');
        $('#qn-chart').highcharts().setSize($('#qn-chart').width(),$('#qn-chart').height());
    });


    /**
     *  初始化图表
     * */
    function initPage() {
        initSlider();
        initChart();
        initPopover();
    }

    //初始化slider
    function initSlider() {
        $('#jcb-qn-slider').slider();
    }

    // 构建图表
    function initChart() {
        var colors = Highcharts.getOptions().colors,
            categories = ['MSIE', 'Firefox', 'Chrome', 'Safari', 'Opera'],
            name = 'Browser brands',
            data = [{
                color: colors[1],
                drilldown: {
                    name: 'MSIE versions',
                    categories: ['股票基金'],
                    data: [39.65]
                }
            }, {
                color: colors[0],
                drilldown: {
                    name: 'Firefox versions',
                    categories: ['债券基金'],
                    data: [5.43]
                }
            }, {
                color: colors[8],
                drilldown: {
                    name: 'Chrome versions',
                    categories: ['货币基金'],
                    data: [41.35]
                }
            }, {
                color: colors[3],
                drilldown: {
                    name: 'Safari versions',
                    categories: ['信托'],
                    data: [14.55]
                }
            }, {
                color: colors[4],
                drilldown: {
                    name: 'Opera versions',
                    categories: ['现金'],
                    data: [1.65]
                }
            }];

        // Build the data arrays
        var versionsData = [];
        for (var i = 0; i < data.length; i++) {
            // add version data
            for (var j = 0; j < data[i].drilldown.data.length; j++) {
                var brightness = 0.2 - (j / data[i].drilldown.data.length) / 5;
                versionsData.push({
                    name: data[i].drilldown.categories[j],
                    y: data[i].drilldown.data[j],
                    color: Highcharts.Color(data[i].color).brighten(brightness).get()
                });
            }
        }

        // Create the chart
        $('#qn-chart').highcharts({
            chart: {
                type: 'pie'
            },
            title: {
                text: null
            },
            yAxis: {
                title: {
                    text: null
                }
            },
            plotOptions: {
                pie: {
                    shadow: false,
                    center: ['50%', '50%']
                }
            },
            tooltip: {
                valueSuffix: '%'
            },
            credits: {
                enabled: false
            },
            series: [{
                name: 'Versions',
                data: versionsData,
                size: '60%',
                innerSize: '80%',
                dataLabels: {
                    formatter: function () {
                        // display only if larger than 1
                        return this.y > 1 ? '<b>' + this.point.name + ':</b> ' + this.y + '%' : null;
                    }
                }
            }]
        });
    }

    //特点w
    function initPopover() {
        $('.check-list .check-text').popover();
    }

});